<#assign pojo=oneToManyPojo.getOne()>
<#assign many=oneToManyPojo.getMany()>
<#assign ones=oneToManyPojo.getOnes()>
<#assign fields=pojo.getFilterdPojoFields()>
<#assign declarationName = pojo.importType(pojo.getDeclarationName())>
<#assign declarationNameFirstLetterLower = pojo.importType(pojo.getDeclarationNameFirstLetterLower())>
<#assign realPackageAndClassPath = "${pojo.getRealPackageAndClassPath()}">
<#assign masterid = pojo.getDeclarationNameFirstLetterLower()+"Id">
import {
  save${declarationName},
  delete${declarationName},
  get${declarationName}ById,
  find${declarationName}ByList,
  find${declarationName}ByPage,
<#foreach child in many>
<#assign childdeclarationName = child.importType(child.getDeclarationName())>
<#assign childdeclarationNameFirstLetterLower = child.importType(child.getDeclarationNameFirstLetterLower())>
  save${childdeclarationName},
  delete${childdeclarationName},
</#foreach>
} from '../services/${declarationName}Service'
import { routerRedux } from 'dva/router'
import { parse } from 'qs'
import config from '../../utils/config'
import modelExtend from 'dva-model-extend'
import { pageModel } from '../../models/common'

export default modelExtend(pageModel, {

  namespace: '${declarationNameFirstLetterLower}',

  state: {
    ${declarationNameFirstLetterLower}DataSource: [],
    ${declarationNameFirstLetterLower}Pagination: {
      showSizeChanger: true,
      showQuickJumper: true,
      showTotal: total => `Total ${r"${ "}total} Items`,
      current: 1,
      pageSize: config.defaultPageSize,
      total: 0,
    },
    ${declarationNameFirstLetterLower}Item: {},
    ${declarationNameFirstLetterLower}SelectedRowKeys:[],
    ${declarationNameFirstLetterLower}SelectedRows:[],
    ${declarationNameFirstLetterLower}ExpandedRowKeys: [],
<#foreach child in many>
<#assign childdeclarationName = child.importType(child.getDeclarationName())>
<#assign childdeclarationNameFirstLetterLower = child.importType(child.getDeclarationNameFirstLetterLower())>
    ${childdeclarationNameFirstLetterLower}Item: {},
    ${childdeclarationNameFirstLetterLower}ModalType: 'create',
    ${childdeclarationNameFirstLetterLower}Visible: false,
    ${childdeclarationNameFirstLetterLower}EditVisible: false,
    ${childdeclarationNameFirstLetterLower}SelectedRowKeys: [],
    ${childdeclarationNameFirstLetterLower}SelectedRows: [],
    ${childdeclarationNameFirstLetterLower}SelectVisible:false,
</#foreach>
  },

  subscriptions: {
    setup ({ dispatch, history }) {
      history.listen(location => {
        if (location.pathname == '/${declarationNameFirstLetterLower}') {
          dispatch({
            type: 'query',
            payload: location.query
          })
        }
      })
    }
  },

  effects: {
    *query ({ payload = {} }, { call, put }) {
      let {pageIndex, pageSize} = payload;
      pageIndex=(pageIndex == null ? 1 : pageIndex);
      pageSize = (pageSize == null ? config.defaultPageSize : pageSize);
      let query = {...payload, pageIndex, pageSize};
      const data = yield call(find${declarationName}ByPage, parse(query))
      if (data) {
        yield put({
          type: 'querySuccess',
          payload: {
            ${declarationNameFirstLetterLower}DataSource: data.data.result,
            ${declarationNameFirstLetterLower}Pagination: {
              current: Number(payload.pageIndex) || 1,
              pageSize: Number(payload.pageSize) || config.defaultPageSize,
              total: data.data.total,
            },
          },
        })
        yield put({type: 'updateState', payload: {${declarationNameFirstLetterLower}ExpandedRowKeys: []}})
      }
    },

    *'delete' ({ payload }, { call, put, select }) {
      const data = yield call(delete${declarationName}, { id: payload })
      const { ${declarationNameFirstLetterLower}SelectedRowKeys } = yield select(_ => _.${declarationNameFirstLetterLower})
      if (data.success) {
        yield put({ type: 'updateState', payload: { ${declarationNameFirstLetterLower}SelectedRowKeys: ${declarationNameFirstLetterLower}SelectedRowKeys.filter(_ => _ !== payload) } })
        yield put({ type: 'query' })
      } else {
        throw data
      }
    },

    *create ({ payload }, { select, call, put }) {
      const data = yield call(save${declarationName}, payload)
      if (data.success) {
        yield put({ type: 'hideModal' })
        yield put({ type: 'query' })
      } else {
        throw data
      }
    },

    *update ({ payload }, { select, call, put }) {
      const id = yield select(({ ${declarationNameFirstLetterLower} }) => ${declarationNameFirstLetterLower}.${declarationNameFirstLetterLower}Item.id)
      const newEntity = { ...payload, id }
      const data = yield call(save${declarationName}, newEntity)
      if (data.success) {
        yield put({ type: 'hideModal' })
        yield put({ type: 'query' })
      } else {
        throw data
      }
    },

<#foreach child in many>
<#assign childdeclarationName = child.importType(child.getDeclarationName())>
<#assign childdeclarationNameFirstLetterLower = child.importType(child.getDeclarationNameFirstLetterLower())>
    *'deleteTemplate' ({payload}, {call, put, select}) {
      const ${declarationNameFirstLetterLower}Id = yield select(({${declarationNameFirstLetterLower}}) => ${declarationNameFirstLetterLower}.${declarationNameFirstLetterLower}Item.id)
      const ${declarationNameFirstLetterLower}Item = yield select(({${declarationNameFirstLetterLower}}) => ${declarationNameFirstLetterLower}.${declarationNameFirstLetterLower}Item)
      const data = yield call(deleteTemplate, ${declarationNameFirstLetterLower}Id, {id: payload})
      const {${childdeclarationNameFirstLetterLower}SelectedRowKeys} = yield select(_ => _.${declarationNameFirstLetterLower})
      if (data.success) {
        const deleteIds= payload;
        let ${childdeclarationNameFirstLetterLower}s = yield select(({${declarationNameFirstLetterLower}}) => ${declarationNameFirstLetterLower}.${declarationNameFirstLetterLower}Item.${childdeclarationNameFirstLetterLower}s)
        deleteIds.map(function (deleteId) {
          ${childdeclarationNameFirstLetterLower}s = ${childdeclarationNameFirstLetterLower}s.filter(_ => _.id !== deleteId)
        })
        ${declarationNameFirstLetterLower}Item.${childdeclarationNameFirstLetterLower}s = ${childdeclarationNameFirstLetterLower}s
        yield put({type: 'updateState', payload: {${declarationNameFirstLetterLower}Item: ${declarationNameFirstLetterLower}Item}})
        yield put({
          type: 'updateState',
          payload: {${childdeclarationNameFirstLetterLower}SelectedRowKeys: ${childdeclarationNameFirstLetterLower}SelectedRowKeys.filter(_ => _ !== payload)}
        })
        // yield put({ type: 'query' })
      } else {
        throw data
      }
    },

    *createTemplate ({payload}, {call, put, select}) {
      const ${declarationNameFirstLetterLower}Id = yield select(({${declarationNameFirstLetterLower}}) => ${declarationNameFirstLetterLower}.${declarationNameFirstLetterLower}Item.id)
      const data = yield call(saveTemplate, ${declarationNameFirstLetterLower}Id, payload)
      if (data.success) {
        const ${childdeclarationNameFirstLetterLower}s = yield select(({${declarationNameFirstLetterLower}}) => ${declarationNameFirstLetterLower}.${declarationNameFirstLetterLower}Item.${childdeclarationNameFirstLetterLower}s)
        ${childdeclarationNameFirstLetterLower}s.push(data.data);
        yield put({type: 'updateState', payload: {${childdeclarationNameFirstLetterLower}s: ${childdeclarationNameFirstLetterLower}s}});
        yield put({type: 'hideTemplateEditModal'})
      } else {
        throw data
      }
    },

    *updateTemplate ({payload}, {select, call, put}) {
      const ${declarationNameFirstLetterLower}Id = yield select(({${declarationNameFirstLetterLower}}) => ${declarationNameFirstLetterLower}.${declarationNameFirstLetterLower}Item.id)
      const id = yield select(({${declarationNameFirstLetterLower}}) => ${declarationNameFirstLetterLower}.${childdeclarationNameFirstLetterLower}Item.id)
      const newEntity = {...payload, id}
      const data = yield call(saveTemplate, ${declarationNameFirstLetterLower}Id, newEntity)
      if (data.success) {
        const ${declarationNameFirstLetterLower}Item = yield select(({${declarationNameFirstLetterLower}}) => ${declarationNameFirstLetterLower}.${declarationNameFirstLetterLower}Item)
        const ${childdeclarationNameFirstLetterLower}s = ${declarationNameFirstLetterLower}Item.${childdeclarationNameFirstLetterLower}s
        let new${childdeclarationNameFirstLetterLower}s = [];
        ${childdeclarationNameFirstLetterLower}s.map(function (${childdeclarationNameFirstLetterLower}) {
          if (${childdeclarationNameFirstLetterLower}.id != id) {
            new${childdeclarationNameFirstLetterLower}s.push(${childdeclarationNameFirstLetterLower})
          } else {
            new${childdeclarationNameFirstLetterLower}s.push(data.data)
          }
        })
        ${declarationNameFirstLetterLower}Item.${childdeclarationNameFirstLetterLower}s = new${childdeclarationNameFirstLetterLower}s
        yield put({type: 'updateState', payload: {${declarationNameFirstLetterLower}Item: ${declarationNameFirstLetterLower}Item}});
        yield put({type: 'hideTemplateEditModal'})
      } else {
        throw data
      }
    },
</#foreach>

  },

  reducers: {
    querySuccess (state, { payload }) {
      const { ${declarationNameFirstLetterLower}DataSource, ${declarationNameFirstLetterLower}Pagination } = payload
      return {
        ...state,
        ${declarationNameFirstLetterLower}DataSource,
        ${declarationNameFirstLetterLower}Pagination: {
          ...state.${declarationNameFirstLetterLower}Pagination,
          ...${declarationNameFirstLetterLower}Pagination,
        },
      }
    },
    showModal (state, {payload}) {
      return {...state, ...payload, ${declarationNameFirstLetterLower}ModalVisible: true}
    },
    hideModal (state) {
      return { ...state, ${declarationNameFirstLetterLower}ModalVisible: false }
    },
    switchExpand (state) {
      return { ...state, ${declarationNameFirstLetterLower}Rxpand: !state.${declarationNameFirstLetterLower}Rxpand }
    },
<#foreach child in many>
<#assign childdeclarationName = child.importType(child.getDeclarationName())>
<#assign childdeclarationNameFirstLetterLower = child.importType(child.getDeclarationNameFirstLetterLower())>
    hide${childdeclarationName}Modal (state) {
      return {...state, ${childdeclarationNameFirstLetterLower}Visible: false}
    },
    show${childdeclarationName}EditModal (state, {payload}) {
      return {...state, ...payload, ${childdeclarationNameFirstLetterLower}EditVisible: true}
    },
    hide${childdeclarationName}EditModal (state) {
      return {...state, ${childdeclarationNameFirstLetterLower}EditVisible: false}
    },
</#foreach>
  }

})
